﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using Excel = Microsoft.Office.Interop.Excel;
namespace GiaoDien
{
    public partial class ExportDocGia : Form
    {
        private DataTable datatable;
        //private string sqlquery;
        public ExportDocGia()
        {
            InitializeComponent();
        }
        private void init()
        {
            datatable = new DataTable();
           // sqlquery = "";
        }
        private void xemtatca()
        {
            DOCGIABUS dgBUS = new DOCGIABUS();
            datatable = dgBUS.Loaddocgia();
            this.dataGridView1.DataSource = datatable;
        }
        private void ExportDocGia_Load(object sender, EventArgs e)
        {
            init();
            xemtatca();
        }

        private void btnexport_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "Excel file *.xls|*.xls";
            if (save.ShowDialog() == DialogResult.OK)
            {

                Excel.Application xlApp = new Excel.Application();
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;
                xlWorkBook = xlApp.Workbooks.Add(misValue);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                int i = 0;
                int j = 0;

                for (i = 0; i <= dataGridView1.RowCount - 1; i++)
                {
                    for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
                    {

                        DataGridViewCell cell = dataGridView1[j, i];
                        xlWorkSheet.Cells[1, j + 1] = dataGridView1.Columns[j].Name;

                        xlWorkSheet.Cells[i + 2, j + 1] = cell.Value;

                    }
                }

                xlWorkBook.SaveAs(save.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();

                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);

                MessageBox.Show("Excel file created ");
            }
        }
        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }     
    }
}
